package com.maomao.ssoserver.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.maomao.ssoserver.mapper.BaseResourcesMapper;
import com.maomao.ssoserver.mapper.BaseRoleMapper;
import com.maomao.ssoserver.mapper.BaseUserMapper;
import com.maomao.ssoserver.mapper.OauthClientMapper;
import com.maomao.ssoserver.model.BaseResources;
import com.maomao.ssoserver.model.BaseRole;
import com.maomao.ssoserver.model.OauthClient;
import com.maomao.ssoserver.model.UserInfo;
import com.maomao.ssoserver.service.OAuthService;

@Service
public class OAuthServiceImpl implements OAuthService{
	
	@Value("${auoth.token-expires}")
	private long tokenExpires;
	
	@Value("${auoth.code-expires}")
	private long codeExpires;
	
	@Value("${auoth.token-prefix}")
	private String tokenPrefix;
	
	@Value("${auoth.code-prefix}")
	private String codePrefix;
	
	@Autowired
	private OauthClientMapper oauthClientMapper;
	
	@Autowired
	private RedisTemplate<String ,String> redisTemplate;
	
	@Autowired
	private BaseRoleMapper baseRoleMapper;
	
	@Autowired
	private BaseResourcesMapper baseResourcesMapper;
	
	@Autowired 
	private BaseUserMapper baseUserMapper;
	
	//private final BaseUser user = (BaseUser)SecurityUtils.getSubject().getPrincipal(); 

	@Override
	public void addAuthCode(String authCode, String username) {
		
		redisTemplate.opsForValue().set(codePrefix + authCode, username, codeExpires,TimeUnit.SECONDS);
	}

	@Override
	public void addAccessToken(String accessToken, String username) {
		
		redisTemplate.opsForValue().set(tokenPrefix + accessToken, username, tokenExpires,TimeUnit.SECONDS);
	}

	@Override
	public boolean checkAuthCode(String authCode) {
		String userName = redisTemplate.opsForValue().get(codePrefix + authCode);
		if(userName != null){
			return true;
		}
		return false;
	}

	@Override
	public boolean checkAccessToken(String accessToken) {
		String userName = redisTemplate.opsForValue().get(tokenPrefix + accessToken);
		if(userName != null ){
			return true;
		}
		return false;
	}

	@Override
	public String getUsernameByAuthCode(String authCode) {
		String userName = redisTemplate.opsForValue().get(codePrefix + authCode);
		return userName;
	}

	@Override
	public String getUsernameByAccessToken(String accessToken) {
		String userName = redisTemplate.opsForValue().get(tokenPrefix + accessToken);
		return userName;
	}

	@Override
	public long getAuthCodeExpireIn(String authCode) {
		long time = redisTemplate.getExpire(codePrefix  + authCode,TimeUnit.SECONDS);
		return time;
	}

	@Override
	public long getAccessTokenExpireIn(String accessToken) {
		long time = redisTemplate.getExpire(tokenPrefix + accessToken,TimeUnit.SECONDS);
		return time;
	}

	@Override
	public boolean checkClientIdAuth(String clientId) {
		OauthClient client = oauthClientMapper.findByClientId(clientId);
		if(client!=null){
			return true;
		}
		return false;
	}

	@Override
	public boolean checkClientToken(String clientId, String clientSecret) {
		OauthClient client = oauthClientMapper.findByClientId(clientId);
		if(client!=null && client.getClientSecred().equals(clientSecret)){
			return true;
		}
		return false;
	}

	@Override
	public UserInfo userInfo(String accessToken){
		String username = this.getUsernameByAccessToken(accessToken);
		UserInfo userInfo = new UserInfo(baseUserMapper.findbyUsername(username));
		List<BaseRole> roleList = baseRoleMapper.findByUserId(userInfo.getId());
		List<BaseResources> permissionList = baseResourcesMapper.findByUserId(userInfo.getId());
		
		userInfo.setRoles(roleList);
		userInfo.setPermissions(permissionList);
		return userInfo;
	}
}
